package com.sample.wanandroidx.support

import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.Color
import com.azhon.appupdate.config.UpdateConfiguration
import com.azhon.appupdate.manager.DownloadManager
import com.azhon.appupdate.utils.Constant
import com.sample.library.IView
import com.sample.library.util.SPUtils
import com.sample.wanandroidx.R
import java.text.SimpleDateFormat
import java.util.*

data class ApkInfo(
    val downloadLink: String, val force: String, val verCode: String, val verName: String,
)

object UpdateUtil {

    /**
     * 检查更新
     * @param showNewerToast 是否提示用户 "当前已是最新版本"
     */
    fun requestUpdate(iView: IView, showNewerToast: Boolean) {
        /*ApplicationModel.update(iView) {
            val versionCode = SystemUtil.getVersionCode(iView.withContext())
            if (it.verCode.toInt() > versionCode) {
                update(iView.withContext(), it)
            } else {
                if (showNewerToast) {
                    ToastUtil.show("当前已是最新版本")
                }
            }
        }*/
    }

    //检查更新 每天只提示一次
    fun updateOnce(iView: IView) {
        val check = SPUtils.getBoolean("update${getDate()}", true)
        if (check) {
            requestUpdate(iView, false)
        }
        SPUtils.putBoolean("update${getDate()}", false)
    }

    private fun update(context: Context, apkInfo: ApkInfo) {
        //如果为强制更新 每次都提示
        if (apkInfo.force == "1") {
            SPUtils.putBoolean("update${getDate()}", true)
        }
        val configuration = UpdateConfiguration()
            //输出错误日志
            .setEnableLog(true)
            //下载完成自动跳动安装页面
            .setJumpInstallPage(true)
            //设置按钮的颜色
            .setDialogButtonColor(Color.parseColor("#09a8ec"))
            //设置对话框强制更新时进度条和文字的颜色
            .setDialogProgressBarColor(Color.parseColor("#09a8ec"))
            //设置按钮的文字颜色
            .setDialogButtonTextColor(Color.WHITE)
            //设置是否显示通知栏进度
            .setShowNotification(true)
            //设置是否提示后台下载toast
            .setShowBgdToast(true)
            //设置强制更新
            .setForcedUpgrade(apkInfo.force == "1")

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            val channel = NotificationChannel(Constant.DEFAULT_CHANNEL_ID,
                Constant.DEFAULT_CHANNEL_NAME,
                NotificationManager.IMPORTANCE_LOW)
            configuration.notificationChannel = channel
        }
        //        设置下载过程的监听
        //            .setOnDownloadListener(object : OnDownloadListenerAdapter() {
        //                override fun downloading(max: Int, progress: Int) {
        //                    val curr = (progress / max.toDouble() * 100.0).toInt()
        //                }
        //            })

        val manager = DownloadManager.getInstance(context)
        val apkName = context.getString(R.string.app_name) + apkInfo.verName + ".apk"
        manager.setApkName(apkName).setApkUrl(apkInfo.downloadLink).setSmallIcon(R.mipmap.ic_launcher)
            .setConfiguration(configuration).setApkVersionCode(apkInfo.verCode.toInt()).setApkVersionName(apkInfo.verName)
            .setApkDescription("\n\r1：优化了用户体验\n\r2：解决了已知问题").download()
    }

    /**
     * 获取今天的日期
     */
    private fun getDate(): String = SimpleDateFormat("yyyyMMdd", Locale.getDefault()).format(Date())
}